<?php defined('SYSPATH') OR die('No direct access allowed.');

class User_Controller extends Template_Controller {
	const ALLOW_PRODUCTION = FALSE;
	public $template = 'layouts/template_user';

	public function index(){
		$this->template->content = new View('user/create');
		$this->template->content->data_prefix_s=ORM::factory("prefix")->find_all();
		$this->template->content->captcha = Captcha::factory();
		$this->template->title = 'สมัครสมาชิก / Register';
	}

	public function userForm(){
		$this->template = new View('layouts/template_user_register');
		$this->template->content = new View('user/create');
		$this->template->content->captcha = Captcha::factory();
		$this->template->title = 'สมัครสมาชิก / Register';

	}

	public function registerFails(){
		$this->template = new View('layouts/template_user_register');
		$this->template->content = new View('user/create');
		$this->template->content->captcha = Captcha::factory();
		$this->template->title = 'สมัครสมาชิก / Register';

	}

	public function register(){
		// grab relevant $_POST data
		if(Captcha::valid($this->input->post('captcha_response'))){
			$user = ORM::factory('user');
			$user->username = $this->input->post('username');
			$user->password = Auth::instance()->hash_password($this->input->post('password'));
			$user->email = $this->input->post("email");;
			$user->role_id=1;
			$user->save();
			$this->template->title = '';
			// if the user was successfully created...
			if ($user->id!=null) {
				// login using the collected data
				//Auth::instance()->login($username, $password);
				// redirect to somewhere
				url::redirect('user/user/profile');
			}else{
				url::redirect('user/user/registerFails');
			}
		}else{
			echo '<p style="color:red">Wrong answer!</p>';
		}

	}

	public function register_old(){
		// grab relevant $_POST data
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$email = $this->input->post("email");
		// instantiate User_Model and set attributes to the $_POST data
		$user = ORM::factory('user');
		$user->username = $username;
		$user->password = Auth::instance()->hash_password($password);
		$user->email = $email;

		$this->template->title = '';
		// if the user was successfully created...
		$role=ORM::factory('role', array('name' => 'login'));
		$user->role_id = $role->id;
		if ($user->save()) {
			url::redirect('user/user/profile');
		}else{
			url::redirect('user/user/registerFails');
		}

	}

	public function profile(){
		$this->template = new View('layouts/template_user');
		$this->template->content = new View('user/profile');
		//		$user_id = Auth::instance()->get_user()->id;
		//		$username = Auth::instance()->get_user()->username;
		$this->template->title ="";
	}

	public function registerUser(){
		//$this->template->content = new View('user/success');
		//echo $this->input->post("firstname");

		if ($_POST){
			if (Captcha::valid($this->input->post('captcha_response'))){
				//echo '<p style="color:green">Good answer!</p>';
				$user = new User_Model();
				$user->prefix_id=$this->input->post("prefix");
				$user->firstname=$this->input->post("firstname");
				$user->lastname=$this->input->post("lastname");
				$user->username=$this->input->post("username");
				$user->password=$this->input->post("password");
				$user->nickname=$this->input->post("nickname"); //nickname
				$user->email=$this->input->post("email");
				$user->role_id=2;
				$user->save();
				url::redirect("user/user/showUserAll");

			}else{
				echo '<p style="color:red">Wrong answer!</p>';
			}

			// Validate other fields here
		}

	}
	public function showUserAll(){
		$this->template->content = new View('user/show_user_all');
		$this->template->content->data_user_s=ORM::factory("user")->find_all();
		$this->template->title = 'แสดงรายการ User ทั้งหมด';
	}
	/*
	 main login function, return to page if logged in with proper credentials
	 */
	public function login($role=""){
		if (UserAuth::get($role)){
			url::redirect('user/user/profile');
		}else{
			if($post = $this->input->post()){
				// orm user object or $form['username'] could be used
				UserAuth::login($role,$user->username, $post['password']);
				if(UserAuth::get($role)){
					url::redirect('user/user/index');
				}else{
					url::redirect('user/user/profile');
				}
				
			}else{
				url::redirect('user/user/index');
			}
		}
	}

	/*
	 *
	 * This example uses the User ORM model, which provides better validation than using the “Auth::instance()→login()” method (the code from example 1 will fail if username is missing or doesn't exist in the database).
	 */
	public function login2() {
		//Check if already logged in
		if (Auth::instance()->logged_in('login')) {
			url::redirect('index');
		} else if (Auth::instance()->logged_in()) {
			url::redirect('accessdenied'); //User hasn't confirmed account yet
		}

		//Initialize template and form fields
		$view = new View('login');
		$view->username = '';
		$view->password = '';

		//Attempt login if form was submitted
		if ($post = $this->input->post()) {
			if (ORM::factory('user')->login($post)) {
				url::redirect($this->session->get('requested_url'));
			} else {
				$view->username = $post['username']; //Redisplay username (but not password) when form is redisplayed.
				$view->message = in_array('required', $post->errors()) ? 'Username and password are required.' : 'Invalid username and/or password.';
			}
		}

		//Display login form
		$view->render(TRUE);
	}

	// or using a a static method
	public function logout(){
		$session = Session::instance();
		UserAuth::logout($session->get('role'),$session);
		url::redirect('welcome/index');
	}
}
?>
